<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Api\CommonController;
use Illuminate\Http\Request;
use App\lists;
use App\bks;
use App\bkdata;
use App\bkgl;
use App\prices;
use Illuminate\Support\Facades\DB;

class DataController extends CommonController{

    public function tips(){
        $tips = '数据只做参考使用，不对您构成任何投资建议！投资有风险，入市需谨慎！工具继续开发中···';
        return $this->returnJson($tips);
    }

    public function marketStatus(){
        $up = lists::where('c_l', '>', 0)->where('j_ds', '>', 0)->count();

        $pp = lists::where('c_l', '>', 0)->where('j_ds', 0)->count();

        $down = lists::where('c_l', '>', 0)->where('j_ds', '<', 0)->count();

        return $this->returnJson(['up'=> $up, 'pp'=>$pp, 'down'=> $down]);
    }

    public function bksIndex(){
        $hybks = bks::orderBy('bk_ds3','desc')->where('bond_count','>', 0)->where('type',2)->limit(6)->get();
        $gnbks = bks::orderBy('bk_ds3','desc')->where('bond_count','>', 0)->where('type',3)->limit(6)->get();

        $data = [
            ["name"=>"行业板块", "path"=>'',"data"=> $hybks, 'code'=>2],
            ["name"=>"概念板块", "path"=>'',"data"=> $gnbks, 'code'=>3]
        ];

        return $this->returnJson($data);
    }

    public function updateDate(){
        $list = lists::orderBy('updated_at', 'desc')->first('updated_at');

        return $this->returnJson(['updated_at'=> date($list['updated_at'])]);
    }
    /**
     * 首页转债列表
     */
    public function indexList(Request $request){
        $orderField = [0=>'j_bfb',1=>'TRANSFER_PREMIUM_RATIO',2=>'c_e', 3=>'CURRENT_BOND_PRICE'];
        $of = $request->input('of', 0);
        $order = $request->input('o','desc');
        // $page = $request->input('p', 1);
        $lists = lists::orderBy($orderField[$of], $order)->where('c_e','>',0)->paginate(20);

        // print($lists);
        $lists->makeHidden(['czinfo','zqinfo','zydate']);

        $data = $this->jsonString($lists);
        // foreach($lists as $key=>$v){
        //     // $v['options'] = preg_replace('//s*/', '', $v['options']);
        //     $v['options'] = $this->hh($v['options'])//str_replace(array("\r\n", "\r", "\n"), "@kg@", $v['options']); 
        //     $v['options'] = json_decode($v['options'], true);
        //     $data[$key] = $v;
        // }

        // $links = $lists->toArray();
        // , ['current_page'=>$links['current_page'],'last_page'=>$links['last_page']]
        return $this->returnJson($data, 200, ['current_page'=>$lists->currentPage(),'last_page'=>$lists->lastPage()]);

    }

    public function bksList(Request $request){
        $type = $request->input('type',0);
        // #概念板块：3
        // #行业板块：2
        // #地区板块：1
        //0 是全部
        $typeArray = [0,2,3,1];
        $order = $request->input('order', 'desc');
        $orderField = $request->input('order_f', 0);
        $orderFields = ['bk_ds3','bond_up','bond_j_bfb','bk_ds'];
        $bks = bks::orderBy($orderFields[$orderField],$order)->where('bond_count','>', 0);


        if($type > 0) $bks = $bks->where('type',$typeArray[$type]);

        $bksList = $bks->paginate(20);


        return $this->returnJson($bksList->items(), 200, ['current_page'=>$bksList->currentPage(),'last_page'=>$bksList->lastPage(), 'page_size'=>20]);
    }

    /**
     * 板块详细信息
     */
    public function bksItem(Request $request){
        $code = $request->input('code', 0);
        if(empty($code)){
            return $this->returnJsonErr('参数错误！');
        }

        $item = bks::where('bk_code',$code)->first();
        $dayItem = bkdata::where('bk_code', $code)->orderBy('created_at', 'asc')->limit(60)->get();//->paginate(30);

        return $this->returnJson(['item'=>$item, 'day_items'=>$dayItem]);
    }

    public function bkBondList(Request $request){

        $orderField = [0=>'j_bfb',1=>'TRANSFER_PREMIUM_RATIO',2=>'c_e', 3=>'CURRENT_BOND_PRICE'];
        $of = $request->input('of', 0);
        $order = $request->input('o','desc');
        $bk_code = $request->input('code', 0);

        if(empty($bk_code)){
            return $this->returnJsonErr('参数错误！');
        }

        $bondCodes = bkgl::where("bk_code", "LIKE","%{$bk_code}%")->get(['bond_code']);

        // print_r($bondCodes);
        $list = lists::orderBy($orderField[$of], $order)->whereIn("bond_code",$bondCodes)
        ->where('c_l', '>', 0)->paginate(20);

        $list->makeHidden(['czinfo','zqinfo','zydate']);

        $data = $this->jsonString($list);

        return $this->returnJson($data ,200, ['current_page'=>$list->currentPage(),'last_page'=>$list->lastPage(),'page_size'=>20]);
    }

    public function bondItem(Request $request){
        $code = $request->input('code', 0);
        if(empty($code)){
            return $this->returnJsonErr('参数错误！');
        }

        $item = lists::where('bond_code', $code)->first();
        $item->options = json_decode($this->hh($item->options),true);
        $item->zqinfo = json_decode($this->hh($item->zqinfo),true);
        $item->czinfo = json_decode($this->hh($item->czinfo),true);
        $item->zydate = json_decode($this->hh($item->zydate),true);

        $bk_code = bkgl::where('bond_code', $code)->first(['bk_code']);
        $bk_code_arr =explode(',',$bk_code->bk_code);
        $bks = bks::whereIn('bk_code', $bk_code_arr)->get();

        $dayData = prices::where('bond_code', $code)->orderBy('created_at', 'asc')->limit(60)->get();


        return $this->returnJson($item, 200, ["bks"=>$bks, 'day_data'=>$dayData]);
        
    }
}